"use strict";
var Transform = function() {
    
};

Transform.prototype = {

    vrModeClientCoords: function(e,_isStereoEnable) {
        var clientX,clientY;
        if (_isStereoEnable) {
          //GET REAL POSITION OF OBJECT
          var middleX = window.innerWidth/2;
          //Right screen
          if(e.clientX>middleX){
              clientX = e.clientX - middleX/2;
          }
          //Left screen
          else {
              clientX = e.clientX + middleX/2;
          } 
          clientY = e.clientY;  //Keep same Y coordinate
          
        }else {
          clientX = e.clientX;
          clientY = e.clientY;
        }

        return {clientX,clientY};
    },

    // 经纬度转换函数
    sphericalCoordsToVector3: function(longitude, latitude, radius) {
        return new THREE.Vector3(
          radius * -Math.cos(latitude) * Math.sin(longitude),
          radius * Math.sin(latitude),
          radius * Math.cos(latitude) * Math.cos(longitude)
        );
    },

    viewerCoordsToVector3: function(viewerPoint,scene,renderer,camera) {
        var screen = new THREE.Vector2(
          2 * viewerPoint.x / renderer.domElement.clientWidth - 1,
          -2 * viewerPoint.y / renderer.domElement.clientHeight + 1
        );
        var raycaster = new THREE.Raycaster();
        raycaster.setFromCamera(screen, camera);

        var intersects = raycaster.intersectObjects(scene.children);

        if (intersects.length === 1) {
          return intersects[0].point;
        }
        else {
          return null;
        }
    },

    vector3ToSphericalCoords: function(vector) {
             
        var twoPI = Math.PI * 2.0;
        var halfPI = Math.PI * 0.5;
        
        var phi = Math.acos(vector.y / Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z));
        var theta = Math.atan2(vector.x, vector.z);

        theta = theta < 0 ? -theta : twoPI - theta;
        phi = halfPI - phi;
        return {
          longitude: theta,
          latitude: phi
        };
      },

    vector3ToViewerCoords: function(vector) {
        vector = vector.clone();
        vector.project(this.prop.camera);

        return {
          x: parseInt((vector.x + 1) / 2 * container.clientWidth),
          y: parseInt((1 - vector.y) / 2 * container.clientHeight)
        };
    },


};